package com.ruoyi.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.app.domain.ShopUser;
import com.ruoyi.app.domain.co.user.SysUserVo;
import com.ruoyi.app.domain.vo.ShopUserVo;
import com.ruoyi.app.mapper.ShopUserMapper;
import com.ruoyi.app.service.IShopUserService;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.MybatisUtils;
import com.ruoyi.project.system.domain.SysUser;
import com.ruoyi.project.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 店铺用户管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-07-07
 */
@Service
public class ShopUserServiceImpl extends ServiceImpl<ShopUserMapper, ShopUser> implements IShopUserService
{
    @Autowired
    private ShopUserMapper shopUserMapper;

    @Autowired
    private ISysUserService userService;

    /**
     * 查询店铺用户管理
     * 
     * @param id 店铺用户管理主键
     * @return 店铺用户管理
     */
    @Override
    public ShopUser selectShopUserById(String id)
    {
        return baseMapper.selectById(id);
    }

    /**
     * 查询店铺用户管理列表
     * 
     * @param shopUser 店铺用户管理
     * @return 店铺用户管理
     */
    @Override
    public List<ShopUser> selectShopUserList(ShopUser shopUser)
    {
        return MybatisUtils.list(baseMapper, shopUser);
    }

    /**
     * 查询店铺用户管理列表
     *
     * @param userId 店铺用户管理
     * @return 店铺用户管理
     */
    @Override
    public List<ShopUserVo> listShopUserByUserId(String userId)
    {
        return baseMapper.listShopUserByUserId(userId);
    }


    /**
     * 修改店铺用户管理
     *
     * @param shopUserVo 店
     **/
    @Override
    public void updateUserShop(ShopUserVo shopUserVo) {
        LambdaQueryWrapper<ShopUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShopUser::getUserId,shopUserVo.getUserId());
        queryWrapper.eq(ShopUser::getShopId,shopUserVo.getShopId());
        queryWrapper.eq(ShopUser::getDelFlag, Constants.NOT_DEL);
        Integer i = baseMapper.selectCount(queryWrapper);
        if (i <= 0) {
            throw new ServiceException("该用户已绑定该店铺");
        }
        SysUser sysUser = new SysUser();
        sysUser.setId(shopUserVo.getUserId());
        sysUser.setShopId(shopUserVo.getShopId());
        userService.updateById(sysUser);
    }


}
